package handler

import (
	"github.com/gin-gonic/gin"
	"repo-sys/model"
)

func Add(c *gin.Context) {

	title := c.PostForm("title")
	content := c.PostForm("content")

	if title == "" || content == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}

	data := model.Article{
		Title:   title,
		Content: content,
	}
	err := model.DB.Create(&data).Debug().Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	} else {
		c.JSON(200, gin.H{
			"code": 200,
			"msg":  "添加成功",
		})
		return
	}
}

func List(c *gin.Context) {

	var article []model.Article

	model.DB.Model(&model.Article{}).Find(&article).Debug()

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "列表获取成功",
		"data": article,
	})
	return
}
